package com.huika.renrench.control.login.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.Response.Listener;
import com.google.gson.reflect.TypeToken;
import com.hk.app.android.lib.encrypt.MD5Security;
import com.huika.lib.validation.EditTextValidator;
import com.huika.lib.validation.ValidationModel;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.Constant;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.login.event.LoginEvent;
import com.huika.renrench.control.login.validations.PassWordValidation;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.support.bean.FindPwdVerfyBean;
import com.huika.renrench.utils.LoginUtils;
import com.huika.renrench.view.TitleBarHelper;
import com.huika.renrench.view.clearedit.ClearableEditText;

import de.greenrobot.event.EventBus;

public class SetNewPwdActivity extends BaseAct implements Listener<RequestResult<String>> {
	private ClearableEditText etPwd;
	private ClearableEditText etConPwd;
	private Button btn;
	private EditTextValidator editTextValidator;

	private FindPwdVerfyBean findPwdVerfyBean;
	private String phone;
	private String pwd;

	@Override
	public void setRootView() {
		setContentView(R.layout.activity_set_newpwd);
		EventBus.getDefault().register(this);
	}

	public void onEventMainThread(LoginEvent loginEvent) {
		if (loginEvent.getLoginStatus() == 1) {
			this.finish();
		}
	}

	@Override
	protected void onDestroy() {
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	}

	@Override
	protected void initData() {
		Intent intent = getIntent();
		findPwdVerfyBean = (FindPwdVerfyBean) intent.getSerializableExtra("FindPwdVerfyBean");
		phone = intent.getStringExtra("phone");
	}

	@Override
	protected void initWidget() {
		TitleBarHelper titleBarHelper = new TitleBarHelper(this, -1, -1, R.string.title_set_newpwd);
		titleBarHelper.setLeftMsg(R.string.left_back);
		etPwd = (ClearableEditText) findViewById(R.id.et_pwd_asn);
		etConPwd = (ClearableEditText) findViewById(R.id.et_conpwd_asn);
		btn = (Button) findViewById(R.id.btn_asn);
		etPwd.addTextChangedListener(new MyPassWordWatcher(etPwd));
		etConPwd.addTextChangedListener(new MyPassWordWatcher(etConPwd));
		btn.setOnClickListener(this);
		editTextValidator = new EditTextValidator(this).add(new ValidationModel(etPwd, new PassWordValidation())).execute();
	}

	class MyPassWordWatcher implements TextWatcher {
		private EditText editText;
		private String tempStr;

		public MyPassWordWatcher(EditText editText) {
			this.editText = editText;
		}

		@Override
		public void afterTextChanged(Editable s) {
			if (tempStr.indexOf(" ") != -1) {
				editText.setText(tempStr.trim());
				editText.setSelection(tempStr.length());
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			if (s.toString().equals(" ")) {
				editText.setText("");
			} else {
				tempStr = s.toString();
			}
		}

	}

	@Override
	public void widgetClick(View v) {
		net();
	}

	private void net() {
		if (editTextValidator.validate()) {
			String conPwdStr = getInputStr(etConPwd);
			pwd = getInputStr(etPwd);
			if (!pwd.equals(conPwdStr)) {
				showToastMsg("两次密码不一致！");
				return;
			}
			FormResultRequest<String> request = new FormResultRequest<String>(UrlConstants.FINDPASSWORDRESET, this, this, new TypeToken<RequestResult<String>>() {
			}.getType());
			JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
			ajaxParams.putStringTypeParam("userId", findPwdVerfyBean.getUid());
			ajaxParams.putStringTypeParam("phone", phone);
			ajaxParams.putStringTypeParam("key", findPwdVerfyBean.getKey());
			ajaxParams.putStringTypeParam("password", MD5Security.getMd5_32_UP(pwd));
			ajaxParams.putStringTypeParam("ip", GlobalApp.ip);
			ajaxParams.putStringTypeParam("mac", GlobalApp.mac);
			ajaxParams.putStringTypeParam("dev", Constant.DEV);
			request.setRequestParams(ajaxParams);
			executeRequest(request);
		}
	}

	@Override
	public void onResponse(RequestResult<String> arg0) {
		dismissLoadingDialog();
		if (arg0.flag == 1) {
			LoginUtils loginUtils = new LoginUtils(this);
			loginUtils.login(phone, pwd);
		}
		showToastMsg(arg0.msg);
	}

}
